#include "controlleruserconfig.h"

#include  <QSqlDatabase>
#include <QSqlQuery>
#include <iostream>
#include <QVariant>
#include <QSqlRecord>

#include "sql.h"
#include "daouser.h"
#include "daovocabulary.h"

UserConfig::UserConfig()
{
    User queryUser =  UserDao::query();

    //当前已配置过User
    if(queryUser.id() != -1) {
        this->user = queryUser;
    }
    else {
        //当前尚未配置User，新增User配置
        User insertUser;
        int insertId = UserDao::insert(insertUser);
        if (insertId != -1) {
            user.setId(insertId);
        }
        else {
            user.setId(-1);
        }
    }
}
QList<Vocabulary> UserConfig::getAllVocabulary()
{
    return VocabularyDao::queryAll();
}

bool UserConfig::updateUserConfig(const User& user)
{
    this->user.setCurrentVocabularyId(user.currentVocabularyId());
    this->user.setCurrentVocabularyName(user.currentVocabularyName());
    this->user.setCurrentGroup(user.currentGroup());

    return UserDao::update(user);
}

bool UserConfig::isUserConfiged()
{
    if(user.id() != -1 && user.currentVocabularyId() != -1 && user.groupSize() != -1) {
        return true;
    }
    else {
        return false;
    }

}


UserConfig& UserConfig::getInstance()
{
    static UserConfig userConfig;
    return userConfig;
}

User UserConfig::getUser()
{
    return this->user;
}
UserConfig::~UserConfig()
{

}
